﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gunningine
{
    namespace Rendersystems
    {
        namespace OpenGL
        {
            using OpenTK;
            using OpenTK.Graphics;
            using OpenTK.Graphics.OpenGL;
            using Gunningine;

            public class RenderTextureGL : RenderTexture, IDisposable
            {
                private TextureGL texture;

                private RenderSystem renderSystem;

                /// <summary>
                /// Constructor.
                /// </summary>
                /// <param name="width"></param>
                /// <param name="height"></param>
                /// <param name="channels"></param>
                public RenderTextureGL(int width, int height, int channels, RenderSystem renderSystem)
                    : base(width, height, channels)
                {
                    this.renderSystem = renderSystem;
                    this.Id = GL.GenFramebuffer();
                    Bind();

                    this.texture = new TextureGL(width, height, channels, (IntPtr)0);

                    // Set "renderedTexture" as colour attachment #0
                    GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, this.texture.Id, 0);
                    GL.DrawBuffer(DrawBufferMode.ColorAttachment0);

                    if (GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferErrorCode.FramebufferComplete)
                        throw new Exception("Could not create framebuffer for render texture!");

                    Unbind();
                }

                /// <summary>
                /// Destructor.
                /// </summary>
                ~RenderTextureGL()
                {
                    Dispose();
                }

                /// <summary>
                /// Bind render texture.
                /// </summary>
                public override void Bind()
                {
                    GL.BindFramebuffer(FramebufferTarget.Framebuffer, this.Id);
                    GL.Viewport(0, 0, this.Width, this.Height);
                }

                /// <summary>
                /// Unbind render texture.
                /// </summary>
                public override void Unbind()
                {
                    GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
                    GL.Viewport(this.renderSystem.ScreenLeft, this.renderSystem.ScreenBottom,
                                this.renderSystem.ScreenWidth, this.renderSystem.ScreenHeight);
                }

                /// <summary>
                /// Dispose.
                /// </summary>
                protected override void DisposeImpl()
                {
                    //GL.DeleteFramebuffer(this.Id);
                    //this.texture.Dispose();
                }
            }
        }
    }
}
